import { getSomeData } from '@/utils/timeUtils.js'
export default {
  data() {
    return {
      total: 1,
      pageSize: 15,
      loading: false,
      tableData: [],
      currentPage: 1,
      tableUrl: '',
      entity: '',
      mountedType: false,
    }
  },
  created() {},
  mounted() {
    let _this = this
    // console.log(_this.total)
    if (!_this.mountedType) {
      _this.refreshTable()
    }
  },
  methods: {
    //刷新Table
    refreshTable() {
      let _this = this
      _this.currentPage = 1
      _this.getTableData()
    },
    handleTableParam() {
      let _this = this
      let param = {
        pageSize: _this.pageSize,
        currentPage: _this.currentPage,
        entity: _this.entity,
      }
      let search = _this.search
      if (search) {
        let mapStr = new Object()
        for (let name in search) {
          if (search[name]) {
            mapStr[name] = search[name]
          }
        }
        param.mapStr = JSON.stringify(mapStr)
      }
      return param
    },
    getDisplay(a) {
      // let _this = this
      let s = a.column.property
      let res = a.row[s]
      let lowS = s.toLowerCase()
      if (lowS == 'createtime') {
        res = getSomeData(res)
      }
      return res
    },
    //获取当前页数据
    async getTableData() {
      let _this = this
      let param = _this.handleTableParam()
      if (!param) {
        return
      }
      _this.loading = true
      let res
      if (_this.tableMethods) {
        res = await _this.$api.getRequest(param, _this.tableUrl)
      } else {
        res = await _this.$api.postRequest(param, _this.tableUrl)
        // console.log(res)
      }
      _this.loading = false
      if (res.code) {
      } else {
        _this.total = res.total
        _this.handleResultTableData(res)
      }
      let table = this.$refs.table
      if (table) {
        let scrollTop = table.bodyWrapper
        if (scrollTop) {
          scrollTop.scrollTop = 0
        }
      }
    },
    //处理tabledata
    handleResultTableData(res) {
      let _this = this
      // _this.tableData = res.list
      // console.log(res.list)
      if (res.list) {
        if (_this.tableUrl == 'getPortList') {
          res.list.forEach((item) => {
            if (
              item.name == 'All IANA assigned TCP and UDP 2012-02-10' ||
              item.name == 'All IANA assigned TCP 2012-02-10' ||
              item.name == 'All privileged TCP' ||
              item.name == 'All privileged TCP and UDP' ||
              item.name == 'All TCP' ||
              item.name == 'All TCP and Nmap 5.51 top 100 UDP' ||
              item.name == 'All TCP and Nmap 5.51 top 1000 UDP' ||
              item.name == 'Nmap 5.51 top 2000 TCP and top 100 UDP' ||
              item.name == 'OpenVAS Default'
            ) {
              item.inUse = 1
              item.default = 1
            }
          })
          _this.tableData = res.list
          // inUse: 1
        } else {
          _this.tableData = res.list
        }
      } else {
        _this.tableData = res
      }
    },
    //更换页码
    changePage(val) {
      let _this = this
      _this.getTableData()
      //console.log(val)
    },
  },
}
